package com.beans;

import java.util.Collection;
import java.util.Date;
import java.util.Random;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

@Entity
@javax.persistence.Table(name="POKER_TABLE")
public class Table 
{	
	@Id @GeneratedValue
	@Column(name="TBL_ID")
	private int tableId;
	
	private Date created;
	
	@Column(name="TBL_NAME")
	private String name;
	
	@Column(name="TBL_COUNT")
	private int count;
	
	@OneToMany(mappedBy="table")
	private Collection<Place> places;
	
	@OneToMany(mappedBy="table")
	private Collection<Round> rounds;
	
	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "POKER_TJ_CARD_TABLE", 
		joinColumns = { @JoinColumn(name = "TBL_ID") }, 
		inverseJoinColumns = { @JoinColumn(name = "CRD_ID") }
	)
	private Collection<Card> cards;
	
	
	
	public Collection<Card> getCards() {
		return cards;
	}

	public void setCards(Collection<Card> cards) {
		this.cards = cards;
	}

	public Collection<Round> getRounds() {
		return rounds;
	}

	public void setRounds(Collection<Round> rounds) {
		this.rounds = rounds;
	}

	public Collection<Place> getPlaces() {
		return places;
	}

	public void setPlaces(Collection<Place> places) {
		this.places = places;
	}

	/**
	 * Return a random name for a table
	 * @return name
	 */
	public static String getRandomName()
	{
	   Random r = new Random();
	   int valeurMin = 0;
	   int valeurMax = 9;
	   String prefix = "T_";
	   String vocals = "aeiouyh" + "aeiou" + "aeiou";
	   String cons = "bcdfghjklmnpqrstvwxz" + "bcdfgjklmnprstvw" + "bcdfgjklmnprst";
	   String allchars = vocals + cons;

	   int length = 6;
	   
	   if (length < 1) length = 1;
	   int consnum = 0;

	   if (prefix.length() > 0) {
	      for (int i = 0; i < prefix.length(); i++) {
	         if (consnum == 2) consnum = 0;
	         if (cons.indexOf(prefix.indexOf(i)) != -1) {
	            consnum++;
	         }
	      }
	   }
	   else {
	      consnum = 1;
	   }
	   
	   String name = prefix;
	   String touse;
	   for (int i = 0; i < length; i++)
	   {
	      //if we have used 2 consonants, the next char must be vocal.
	      if (consnum == 2)
	      {
	         touse = vocals;
	         consnum = 0;
	      }
	      else touse = allchars;
	      //pick a random character from the set we are goin to use.
	      
	      char c = touse.charAt(r.nextInt(valeurMax - valeurMin));
	      name = name + c;
	      if (cons.indexOf(c) != -1) consnum++;
	   }
	   name = name.charAt(0) + name.substring(1, name.length());
	   return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public void setTableId(int tableId) {
		this.tableId = tableId;
	}

	public int getTableId() {
		return tableId;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}
	
}
